package com.songlanyun.modules.rights.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.OrderException;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.rights.dao.RightsOrderShipRecordDao;
import com.songlanyun.modules.rights.entity.RightsOrderShipRecord;
import com.songlanyun.modules.rights.service.RightsOrderShipRecordService;


@Service("saleRightsOrderShipRecordService")
public class RightsOrderShipRecordServiceImpl extends ServiceImpl<RightsOrderShipRecordDao, RightsOrderShipRecord> implements RightsOrderShipRecordService {

    @Override
    public RightsOrderShipRecord create(RightsOrderShipRecord entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public RightsOrderShipRecord modify(RightsOrderShipRecord entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RightsOrderShipRecord> page = this.page(
                new Query<RightsOrderShipRecord>().getPage(params),
                new QueryWrapper<RightsOrderShipRecord>()
        );

        return new PageUtils(page);
    }

    @Override
    public RightsOrderShipRecord loadByRightsId(Long rightsId, boolean tw) {
        RightsOrderShipRecord one = this.getOne(new LambdaQueryWrapper<RightsOrderShipRecord>()
                .eq(RightsOrderShipRecord::getRightsOrderId, rightsId)
                .last("limit 1"));
        if(one==null && tw)
        {
             //退款退货维权单还没有发货
            throw new RRException(OrderException.RIGHTS_NOT_SHIP);
        }
        return one;
    }

}